<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Push extends MY_Controller
{
	private $coins;

	public function __construct()
	{
		parent::__construct();
		$this->coins = $this->d->rows(array('table' => 'coin', 'where' => 'status=1', 'order' => 'sort asc'));
	}

	public function index()
	{
		$coin_id = intval($this->input->get('cid'));
		$where = '';

		if ($coin_id) {
			$where = 'coin_id=' . $coin_id;
		}

		$rows = $this->result_list(array('table' => 'push', 'where' => $where, 'order' => 'add_time desc', 'url' => 'push/index', 'per_page' => 20));

		foreach ($rows['list'] as $k => $v) {
			$rows['list'][$k]['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $v['coin_id']));
			$rows['list'][$k]['price'] = num($v['total'] / $v['amount']);
			$rows['list'][$k]['percent'] = num(($v['deal'] / $v['amount']) * 100);
		}

		$d['list'] = $rows['list'];
		$d['page'] = $rows['page'];
		$d['coins'] = $this->coins;
		$d['coin_id'] = $coin_id;
		$this->show(array('header', 'push', 'footer'), $d);
	}

	public function form()
	{
		$id = intval($this->input->get('id'));
		$id = intval($this->input->get('id'));

		if (!$id) {
			error_404();
		}

		$d = $this->d->find(array('table' => 'push', 'where' => 'id=' . $id));

		if (!$d) {
			error_404();
		}

		$this->show('push_trade', $d);
	}

	public function trade()
	{
		if ($this->cfg['is_push'] == 0) {
			ajax_return(1, '系统关闭中');
		}

		$this->verify_user_ajax();
		$paypwd = $this->input->post('paypwd');
		if (!$paypwd || !$this->pwd->verify($paypwd, $this->user['paypwd'])) {
			ajax_return(1, '安全密码错误');
		}

		$id = intval($this->input->post('id'));

		if (!$id) {
			ajax_return(1, '找不到交易对象');
		}

		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_xnb write, t_push write, t_deal write, t_coin write');
		$push = $this->d->find(array('table' => 'push', 'where' => 'id=' . $id . ' and status=0'));

		if (!$push) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到交易对象');
		}

		$coin_id = $push['coin_id'];

		if (!$coin_id) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到交易币种');
		}

		$coin = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $coin_id));

		if (!$coin) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到投资币种');
		}

		$price_coin_id = $push['price_coin_id'];

		if (!$price_coin_id) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到支付币种');
		}

		$price_coin = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $price_coin_id));

		if (!$price_coin) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到支付币种');
		}

		$price = num($push['price']);

		if (!$price) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '价格过低');
		}

		$amount = num($this->input->post('amount'));

		if (!$amount) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '数量过低');
		}

		$amount = num($this->input->post('amount'));

		if ($push['amount'] < $amount) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '数量过高');
		}

		$total = num($push['price'] * $amount);

		if (!$total) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '总额过低');
		}

		$xnb = $this->d->field(array('field' => 'num', 'table' => 'xnb', 'where' => 'user_id=' . $this->user['id'] . ' and coin_id=' . $coin_id));

		if ($xnb < $amount) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '余额不足');
		}

		$rs[] = $this->db->set('num', 'num_frozen-' . $total, false)->where('user_id', $push['id'])->where('coin_id', $price_coin_id)->update('xnb');
		$rs[] = $this->db->set('num', 'num+' . $amount, false)->where('user_id', $push['id'])->where('coin_id', $coin_id)->update('xnb');
		$rs[] = $this->db->set('num', 'num+' . $total, false)->where('user_id', $this->user['user_id'])->where('coin_id', $price_coin_id)->update('xnb');
		$rs[] = $this->db->set('num', 'num-' . $amount, false)->where('user_id', $this->user['user_id'])->where('coin_id', $coin_id)->update('xnb');
		$data = array('push_id' => $id, 'buy_user_id' => $this->user['id'], 'sell_user_id' => $push['user_id'], 'price' => $price, 'amount' => $amount, 'total' => $total, 'price_coin_id' => $price_coin_id, 'coin_id' => $coin_id, 'add_time' => time());
		$rs[] = $this->db->insert('deal', $data);
		$rs[] = $this->db->set('deal', 'deal+' . $amount, false)->where('id', $id)->update('push');
		$rs[] = $this->db->set('status', 1)->where('id', $id)->where('deal>=amount')->update('push');

		if (correct($rs)) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			ajax_return(0, '提交成功');
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '提交失败');
		}
	}
}

?>
